import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_study/config/ConstColor.dart';
import 'package:flutter_study/model/news_model.dart';
import 'package:flutter_study/page/web_page.dart';
import 'package:flutter_study/util/navigator_manager.dart';

class NewsContentWidgetItem extends StatelessWidget {
  final NewsItemModel model;

  const NewsContentWidgetItem({Key key, @required this.model})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () {
          var url = Uri.decodeComponent(model.data.actionUrl
              .substring(model.data.actionUrl.indexOf("url")));
          url = url.substring(4, url.length);
          NavigatorManager.to(WebPage(url: url));
        },
        child: Padding(
          padding: EdgeInsets.all(10),
          child: PhysicalModel(
            color: Color(0xffededed),
            clipBehavior: Clip.antiAlias,
            borderRadius: BorderRadius.circular(4),
            child: Padding(
              padding: EdgeInsets.only(bottom: 10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(
                    padding: EdgeInsets.only(bottom: 10),
                    child: CachedNetworkImage(
                      width: MediaQuery.of(context).size.width,
                      height: 140,
                      imageUrl: model.data.backgroundImage,
                      fit: BoxFit.fill,
                      errorWidget: (context, url, error) {
                        return Image.asset("images/img_load_fail.png");
                      },
                    ),
                  ),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: _getTitleList(model.data.titleList),
                  )
                ],
              ),
            ),
          ),
        ));
  }

  _getTitleList(List<String> titleList) {
    return titleList.map((e) {
      return Padding(
          padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
          child: Text(
            e,
            style: TextStyle(color: ConstColor.desTextColor, fontSize: 12),
            maxLines: 3,
            overflow: TextOverflow.ellipsis,
          ));
    }).toList();
  }
}

